package com.skill.shareskill.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import com.astuetz.PagerSlidingTabStrip;
import com.skill.shareskill.R;

import java.lang.reflect.Field;

/**
 * Created by Administrator on 2018/1/31 0031.
 */

public class FindFragment extends Fragment{
    private PagerSlidingTabStrip tabs;
    private DisplayMetrics dm;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, null);
        setOverflowShowingAlways();
        dm = getResources().getDisplayMetrics();
        ViewPager pager = (ViewPager)view.findViewById(R.id.pager_find);
        pager.setOffscreenPageLimit(8);//设置ViewPager的缓存界面数,默认缓存为2
        tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs_find);
        setTabsValue();
        pager.setAdapter(new FindFragment.MyPagerAdapter(getChildFragmentManager()));
        tabs.setViewPager(pager);
        return view;
    }
    private void setTabsValue() {
        // 设置Tab是自动填充满屏幕的
        tabs.setShouldExpand(true);
        // 设置Tab的分割线是透明的
        tabs.setDividerColor(Color.TRANSPARENT);
        // 设置Tab底部线的高度
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, dm));
        // 设置Tab标题文字的大小
        tabs.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, dm));
        // 设置Tab Indicator的颜色
        tabs.setIndicatorColor(Color.parseColor("#d83737"));//#d83737   #d83737(绿)
        // 设置选中Tab文字的颜色 (这是我自定义的一个方法)
        tabs.setSelectedTabTextColor(Color.parseColor("#E38615"));
        // 取消点击Tab时的背景色
        tabs.setTabBackground(0);
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        private final String[] titles = { "关注", "同城","摄影","旅游","冒险" ,"荒野","海洋","其他"};
        FindAttentionFragment findAttentionFragment;
        FindNearbyFragment findNearbyFragment;
        FindPhotoGraphyFragment findPhotoGraphyFragment;
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    findAttentionFragment = new FindAttentionFragment();
                    return findAttentionFragment;
                case 1:
                        findNearbyFragment = new FindNearbyFragment();
                    return findNearbyFragment;
                case 2:
                    //与fragment的通信
                    findPhotoGraphyFragment = new FindPhotoGraphyFragment();
                    return findPhotoGraphyFragment;
                case 3:
                    findPhotoGraphyFragment = new FindPhotoGraphyFragment();
                    return findPhotoGraphyFragment;
                case 4:
                    findPhotoGraphyFragment = new FindPhotoGraphyFragment();
                    return findPhotoGraphyFragment;
                case 5:
                    findPhotoGraphyFragment = new FindPhotoGraphyFragment();
                    return findPhotoGraphyFragment;
                case 6:
                    findPhotoGraphyFragment = new FindPhotoGraphyFragment();
                    return findPhotoGraphyFragment;
                case 7:
                    findPhotoGraphyFragment = new FindPhotoGraphyFragment();
                    return findPhotoGraphyFragment;
                default:
                    return null;
            }
        }

    }
    private void setOverflowShowingAlways() {
        try {
            ViewConfiguration config = ViewConfiguration.get(getParentFragment().getActivity());
            Field menuKeyField = ViewConfiguration.class
                    .getDeclaredField("sHasPermanentMenuKey");
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
